function avg = CreateAverage(electrNr, subjectNrs)
% we need: the matrixes X and D, output? 

load('Output.mat');
electrodes = GetElectrodes();

% if no electrode argument is provided at call of function then use the
% list of electrodes (in the file GetElectrodes)
if nargin >= 1
    electrodes = electrodes(electrNr);
end

% if no subjects are specified when function is called then use all
% subjects. 
if nargin < 2
    subjectNrs = 1:27;
end

% loop through periods
for p=1:4
    
    %loop through all electrodes
    for e=1:length(electrodes)
        
        for s = subjectNrs  
            % check if the subject has the requested period,
            % if not then provide the X & D (empty) that were supposed 
            % to come out of the period and move on. 
            if length(output.results(i).period)<period)
                X = []; D = [];
                disp('     - Period unavailable...');
                continue;
            end
            
            %interpolate matrix D from a matrix as long as the # of trials,
            %to a matrix that is 1000 columns long
            Di = interp1(X, D, linspace(0,1,size(D,2)), 'spline')
            
            % concatenate along 3rd dimension
            Dall = cat(3, all, Di);
            
        end
        % create empty matrix to store the average values in
        Ae = zeros(8,1000);
        % loop through all the rows
        for r = 1:8
            % loop through all the columns
            for c= 1:1000
                % fill matrix with averages from the 3rd dimension
                Ae = mean (D,3)
            end
        end
        
        
       f = PlotAverage(Dall, 0:1000, period, electrode);
       saveFigure(f, 'J:\PeakAnalysis\Results\', ['P'num2str(p) '-' electrode]);
       close(f);
        
    end
    
end

function [D X] = ResultsSEP(outputOfAnalyze, subjectNr, electrode, period)
    
end 

     